/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PlaylistCellWithArtPanel.java
 *
 * Created on Mar 8, 2009, 2:36:51 PM
 */
package com.bff.mpd.musicjukebox.ui.components;

import com.bff.bjj.ui.component.BJJPanel;
import com.bff.bjj.ui.component.BJJResizeListPanel;
import com.bff.mpd.musicjukebox.lib.ColorProperties;
import com.bff.mpd.musicjukebox.lib.JukeboxProperties;
import com.bff.mpd.utils.Utils;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JList;
import org.bff.javampd.MPDPlaylist;
import org.bff.javampd.objects.MPDAlbum;
import org.bff.javampd.objects.MPDArtist;

/**
 *
 * @author Music
 */
public class AlbumCellPanel extends BJJResizeListPanel {

    private static int currentListSize;
    private MPDPlaylist playlist;
    private MPDAlbum album;
    private MPDArtist artist;
    public static final int PERCENT_TOTAL = 7;
    private int albumLabelSize;

    /** Creates new form PlaylistCellWithArtPanel */
    public AlbumCellPanel(MPDArtist artist,
            MPDAlbum album,
            JList list) {
        super(list);
        this.album = album;
        this.artist = artist;
        this.playlist = JukeboxProperties.getInstance().getPlaylist();

        initComponents();

        //we will add the artist at a later time
        detailPanel.remove(lblBy);
        detailPanel.remove(lblArtist);

        try {
            lblAlbum.setText(album.getName());
        } catch (Exception e) {
            System.out.println("Something is probably null");
            System.out.println("---" + album);
            System.out.println("---" + album.getName());
            System.out.println("---" + artist.getName());
        }

        if (currentListSize == 0) {
            currentListSize = DEFAULT_SIZE;
        }

        createLabels();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        detailPanel = new javax.swing.JPanel();
        lblAlbum = new javax.swing.JLabel();
        lblBy = new javax.swing.JLabel();
        lblArtist = new javax.swing.JLabel();
        jPanel1 = new BJJPanel();
        btnAdd = new AlbumAddButton(getPlaylist(), getAlbum());
        btnAdd.setText(null);

        setLayout(new java.awt.BorderLayout());

        lblAlbum.setText("jLabel1");
        detailPanel.add(lblAlbum);

        lblBy.setForeground(java.awt.Color.gray);
        lblBy.setText(Utils.getMessage("LABEL_BY")); // NOI18N
        detailPanel.add(lblBy);

        lblArtist.setText("jLabel3");
        detailPanel.add(lblArtist);

        add(detailPanel, java.awt.BorderLayout.WEST);

        jPanel1.setLayout(new java.awt.GridBagLayout());

        btnAdd.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/mpd/add.png"))); // NOI18N
        btnAdd.setContentAreaFilled(false);
        jPanel1.add(btnAdd, new java.awt.GridBagConstraints());

        add(jPanel1, java.awt.BorderLayout.EAST);
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAdd;
    private javax.swing.JPanel detailPanel;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JLabel lblAlbum;
    private javax.swing.JLabel lblArtist;
    private javax.swing.JLabel lblBy;
    // End of variables declaration//GEN-END:variables

    public void setEven() {
        setBackground(ColorProperties.LIST_BACKGROUND_EVEN);
    }

    public void setOdd() {
        setBackground(ColorProperties.LIST_BACKGROUND_ODD);
    }

    public void setCurrentPlaying() {
        setForeground(ColorProperties.PLAYLIST_CURRENT_PLAYING_FOREGROUND);
    }

    public void setSelected() {
        setForeground(ColorProperties.LIST_FOREGROUND_SELECTED);
        setBackground(ColorProperties.LIST_BACKGROUND_SELECTED);
        lblBy.setBackground(ColorProperties.LIST_BACKGROUND_SELECTED);
        lblBy.setForeground(ColorProperties.LIST_FOREGROUND_SELECTED);
        showAddButton();
    }

    public void setNotSelected() {
        setForeground(ColorProperties.LIST_FOREGROUND);
        lblBy.setForeground(Color.GRAY);
        hideAddButton();
    }

    @Override
    public void setBackground(Color bg) {
        super.setBackground(bg);
        //detailPanel.setBackground(bg);
        if (detailPanel != null) {
            detailPanel.setBackground(bg);
            jPanel1.setBackground(bg);
        }
    }

    @Override
    public void setForeground(Color fg) {
        super.setForeground(fg);
        if (detailPanel != null) {
            detailPanel.setForeground(fg);
            jPanel1.setForeground(fg);
        }
        setLabelColors(fg);
    }

    private void setLabelColors(Color color) {
        if (lblAlbum != null) {
            lblAlbum.setForeground(color);
            lblArtist.setForeground(color);
        }
    }

    public void setAddButtonVisible(boolean visible) {
        jPanel1.setVisible(visible);
    }

    public void showAddButton() {
        setAddButtonVisible(true);
    }

    public void hideAddButton() {
        setAddButtonVisible(false);
    }

    public MPDPlaylist getPlaylist() {
        return playlist;
    }

    public void setPlaylist(MPDPlaylist playlist) {
        this.playlist = playlist;
    }

    public void setCurrent() {
        setLabelColors(ColorProperties.PLAYLIST_CURRENT_PLAYING_FOREGROUND);
    }

    public void setNotCurrent() {
        setLabelColors(ColorProperties.LIST_FOREGROUND_SELECTED);
    }

    private void createLabels() {
        int total = getCurrentListSize() / PERCENT_TOTAL;

        String album = "";
        try {
            album = getAlbum().getName() == null ? "" : getAlbum().getName();
        } catch (Exception e) {
            e.printStackTrace();
        }

        int totalChars = album.length();

        if (totalChars >= total) {
            setAlbumLabelSize(getCurrentListSize() / PERCENT_TOTAL);
            album = album.length() < getAlbumLabelSize() ? album : album.substring(0, getAlbumLabelSize()) + "...";
        }

        lblAlbum.setText(album + " ");
        lblArtist.setText(artist + " ");

        revalidate();
        repaint();
    }

    /**
     * @return the artistLabelSize
     */
    public int getAlbumLabelSize() {
        return albumLabelSize;
    }

    /**
     * @param artistLabelSize the artistLabelSize to set
     */
    public void setAlbumLabelSize(int albumLabelSize) {
        this.albumLabelSize = albumLabelSize;
    }

    @Override
    public void adjustComponent(Dimension size) {
        setCurrentListSize((int) size.getWidth());
        createLabels();
    }

    /**
     * @return the album
     */
    private MPDAlbum getAlbum() {
        return album;
    }

    /**
     * @param album the album to set
     */
    private void setAlbum(MPDAlbum album) {
        this.album = album;
    }

    /**
     * @return the artist
     */
    public MPDArtist getArtist() {
        return artist;
    }

    /**
     * @param artist the artist to set
     */
    public void setArtist(MPDArtist artist) {
        this.artist = artist;
    }
    
    /**
     * @return the currentListSize
     */
    public static int getCurrentListSize() {
        return currentListSize;
    }

    /**
     * @param aCurrentListSize the currentListSize to set
     */
    public static void setCurrentListSize(int aCurrentListSize) {
        currentListSize = aCurrentListSize;
    }
}
